package com.dimples.dd.system.convert;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dimples.dd.common.model.DDAuthUser;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.util.collection.CollectionUtils;
import com.dimples.dd.common.util.collection.MapUtils;
import com.dimples.dd.system.model.bo.SimpleUserBO;
import com.dimples.dd.system.model.bo.UserBO;
import com.dimples.dd.system.model.bo.UserFormBO;
import com.dimples.dd.system.model.entity.SystemDeptDO;
import com.dimples.dd.system.model.entity.SystemUsersDO;
import com.dimples.dd.system.model.form.UserForm;
import com.dimples.dd.system.model.vo.UserInfoVO;
import com.dimples.dd.system.model.vo.UserPageVO;
import com.dimples.dd.system.model.vo.UserSimpleVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.Mappings;

import java.util.List;
import java.util.Map;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/6/11
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface UserConvert {

    @Mappings({
            @Mapping(source = "id", target = "userId"),
    })
    DDAuthUser toAuthUser(SystemUsersDO systemUsersDO);

    @Mappings({
            @Mapping(target = "genderLabel", expression = "java(com.dimples.dd.common.base.IBaseEnum.getLabelByValue(bo.getGender(), com.dimples.dd.common.enums.GenderEnum.class))")
    })
    UserPageVO bo2Vo(UserBO bo);

    Page<UserPageVO> bo2Vo(Page<UserBO> userBoPage);

    SystemUsersDO form2Entity(UserForm userForm);

    @Mappings({
            @Mapping(target = "userId", source = "id")
    })
    UserInfoVO entity2UserInfoVo(SystemUsersDO user);

    UserForm bo2Form(UserFormBO userFormBO);

    default List<UserSimpleVO> convertSimpleList(List<SystemUsersDO> list, Map<Long, SystemDeptDO> deptMap) {
        return CollectionUtils.convertList(list, user -> {
            UserSimpleVO userVO = BeanUtil.toBean(user, UserSimpleVO.class);
            MapUtils.findAndThen(deptMap, user.getDeptId(), dept -> userVO.setDeptName(dept.getName()));
            return userVO;
        });
    }

    PageResult<Option<Long>> bo2OptionPage(PageResult<SimpleUserBO> pageResult);

    @Mappings({
            @Mapping(source = "id", target = "value"),
            @Mapping(source = "nickname", target = "label")
    })
    Option<Long> bo2Option(SimpleUserBO simpleUserBo);
}

